package com.hello.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.hello.pojo.User;
import com.hello.service.UserService;

@Controller
public class RegisterController {
   
	@Autowired
	private UserService userService;
	
	@RequestMapping("/register")
	public String register() throws Exception{
		return "register";
		
	}
	 @RequestMapping("/registerUserName")
	    @ResponseBody
	    public Map<String,Object> registerUserName(String registerName,HttpServletRequest request){
	        //校验registerUserName方法传入的参数registerName是否为空
	        
	        Map<String,Object> resultMap = new HashMap<String, Object>();  
	        User u = userService.findUserByName(registerName);
	        if(u == null){
	            resultMap.put("result", "success");
	            return resultMap;
	        }else{
	            resultMap.put("result", "notsuccess");
	            return resultMap;
	        }
	    }
	@RequestMapping("/user_checkCode")
	@ResponseBody
	public User checkCode(@RequestBody User user,Model model) throws Exception{
		
		System.out.println(user.getUsername());
		model.addAttribute("result", user);
		
		return user;
		
	}
	
	@RequestMapping("/user_register")
	public String save(User user,Model model) throws Exception{
		userService.checkUser(user);
		
		return "login";
		
	}
}
